﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forum.Web.Models;
using Forum.Web.BaseCode;
using Forum.BLL.Post;
using Forum.BLL.Board;
using Frame.Lib.Web;
using Forum.Entity.Member;
using Forum.BLL.Member;
using Forum.Entity.Post;

namespace Forum.Web.Controllers
{
    public class TopicController : BaseController
    {
        //
        // GET: /Topic/

        private MemberExBO ForumMember
        {
            get
            {
                return MemberExBO.Instance<MemberExBO>();
            }
        }

        private TopicBO ForumTopic
        {
            get
            {
                return TopicBO.Instance<TopicBO>();
            }
        }

        private PostBO ForumPost
        {
            get
            {
                return PostBO.Instance<PostBO>();
            }
        }

        private BoardBO ForumBoard
        {
            get
            {
                return BoardBO.Instance<BoardBO>();
            }
        }

        public ActionResult Add(string boardID)
        {
            string method = this.Request.HttpMethod;

            TopicAddModel model = new TopicAddModel();
            model.BoardInfo = this.ForumBoard.GetInfo(boardID);


            if (method.Equals("get", StringComparison.OrdinalIgnoreCase))
            {
                return View(ForumPath.Views + "Topic/Add.cshtml", model);
            }
            else
            {
                if (base.MemberInfo.IsLogin)
                {
                    string topicID = Request.Form["TopicID"];
                    TopicInfo info = this.ForumTopic.GetInfo(topicID);
                    if (string.IsNullOrEmpty(topicID))
                    {
                        info.InputTime = info.LastPostTime = DateTime.Now;
                    }
                    info.BoardID = int.Parse(boardID);
                    info.IsDisplay = true;
                    info.MemberID = info.LastPosterID = base.MemberInfo.MemberID;
                    info.MemberName = info.LastPosterName = base.MemberInfo.MemberLoginName;
                    string topicType = Request.Form["TopicType"];
                    if(!string.IsNullOrEmpty(topicType))
                    {
                        var tmp = topicType.Split('_');
                        info.TypeID = int.Parse(tmp[0]);
                        info.TypeName = tmp[1];
                    }
                    info.Name = Request.Form["txtTitle"];
                    info.ReplyCount = 1;
              

                    this.ForumTopic.Save(info);

                    if (string.IsNullOrEmpty(topicID))
                    {
                        PostInfo pInfo = new PostInfo();
                        pInfo.MemberID = base.MemberInfo.MemberID;
                        pInfo.BoardID = info.BoardID;
                        pInfo.InputTime = pInfo.ModifyTime = DateTime.Now;
                        pInfo.IsTop = true;
                        pInfo.MemberName = base.MemberInfo.MemberLoginName;
                        pInfo.Name = Request.Form["Content"];
                        pInfo.State = (int)TopicState.Normal;
                        pInfo.TopicID = info.ID;

                        this.ForumPost.Save(pInfo);

                        info.LastPostID = pInfo.ID;
                        info.LastPostTime = DateTime.Now;

                        this.ForumTopic.Save(info);
                    }                    

                    MemberExInfo mInfo = this.ForumMember.GetInfo(base.MemberInfo.MemberID);
                    mInfo.Score += 50;
                    mInfo.PostCount++;
                    this.ForumMember.Save(mInfo);


                    return this.RedirectToAction("List", new { boardID = boardID });

                  
                }
                else
                {
                    return View(ForumPath.Views + "Topic/Add.cshtml", model);

                }


            }

            
        }

       

        public ActionResult List(int boardID)
        {
            string typeID = Request.QueryString["TypeID"];
            TopicListModel model = new TopicListModel();

            model.UrlPrefix = ForumPath.VirtualRoot + "topic/list/" + boardID;
            if (!string.IsNullOrEmpty(typeID))
            {
                model.TopicTypeID = int.Parse(typeID);

                model.UrlPrefix += "?TypeID=" + model.TopicTypeID;
            }

            model.Pager = new PagerSetting();
            model.Pager.PageSize = Paras.TopicPageSize;
            model.Pager.PageIndex = 1;
            if (!string.IsNullOrEmpty(Request.QueryString["PageIndex"]))
            {
                model.Pager.PageIndex = int.Parse(Request.QueryString["PageIndex"]);
            }
           
            model.TopList = this.ForumTopic.GetTopList(boardID);
            model.BoardInfo = (new BoardBO()).GetInfo(boardID);
            model.BoardInfo.Types.Insert(0, new TopicTypeInfo() { ID=0, Name="全部" });

            model.NormalList = this.ForumTopic.GetNormalList(boardID,model.TopicTypeID, model.Pager);

            //model.Pager.RecordCount = 300;


           

            return View(ForumPath.Views + "Topic/List.cshtml", model);
        }

        public ActionResult Detail(int topicID)
        {
            TopicDetailModel model = new TopicDetailModel();

            model.TopicInfo = this.ForumTopic.GetInfo(topicID);
            model.TopicInfo.ViewCount++;//浏览数增加
            this.ForumTopic.Save(model.TopicInfo);

            PostBO post = new PostBO();

            model.UrlPrefix = ForumPath.VirtualRoot + "topic/detail/"+topicID;


            model.Pager = new PagerSetting();
            model.Pager.Where = "TopicID=@p0";
            model.Pager.PageIndex = 1;
            if (!string.IsNullOrEmpty(Request.QueryString["PageIndex"]))
            {
                model.Pager.PageIndex = int.Parse(Request.QueryString["PageIndex"]);
            }
            model.Pager.PageSize = Paras.PostPageSize;
            model.PostList = post.GetList(topicID, model.Pager);
           // model.Pager.RecordCount = 200;

            for (int i = 0; i < model.PostList.Count; i++)
            {
                model.PostList[i].Floor = ((model.Pager.PageIndex - 1) * model.Pager.PageSize) + i + 1;
            }
          

            return View(ForumPath.Views + "Topic/Detail.cshtml", model);
        }

        public ActionResult ReplyPost()
        {

            if (base.MemberInfo.IsLogin)
            {
                int topicID = int.Parse(Request.Form["TopicID"]);

                TopicInfo tInfo = this.ForumTopic.GetInfo(topicID);
                MemberExInfo mInfo = this.ForumMember.GetInfo(base.MemberInfo.MemberID);

                string postID = Request["PostID"];
                PostInfo pInfo = this.ForumPost.GetInfo(postID);
                pInfo.BoardID = tInfo.BoardID;
                pInfo.MemberID = mInfo.ID;
                pInfo.MemberName = mInfo.MemberBaseInfo.LoginName;
                pInfo.Name = Request.Form["Content"];
                if (string.IsNullOrEmpty(postID))
                {
                    pInfo.InputTime = pInfo.ModifyTime = DateTime.Now;                   
                }
                else
                {
                    pInfo.ModifyTime = DateTime.Now;
                }
                pInfo.State = (int)PostState.Normal;
                pInfo.TopicID = tInfo.ID;

                this.ForumPost.Save(pInfo);

                if (string.IsNullOrEmpty(postID))
                {
                    tInfo.ReplyCount += 1;
                    tInfo.LastPosterID = mInfo.ID;
                    tInfo.LastPosterName = mInfo.MemberBaseInfo.LoginName;
                    tInfo.LastPostID = pInfo.ID;
                    tInfo.LastPostTime = DateTime.Now;

                    this.ForumTopic.Save(tInfo);

                    mInfo.LastPostTime = DateTime.Now;
                    mInfo.PostCount++;
                    mInfo.Score += 10;

                    this.ForumMember.Save(mInfo);
                }

            }
            string url = Request.UrlReferrer.AbsolutePath;

           

            return this.Redirect(url);
        }

    }
}
